/* SPDX-License-Identifier: GPL-2.0-or-later */
/* Copyright(c) 2020 - 2023 Allwinner Technology Co.,Ltd. All rights reserved. */
/*
 * A V4L2 driver for Raw cameras.
 *
 * Copyright (c) 2017 by Allwinnertech Co., Ltd.  http://www.allwinnertech.com
 *
 * Authors:  wujiayongs <wujiayongs@allwinnertech.com>
 *    Liang WeiJie <liangweijie@allwinnertech.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */
#include "../../utility/vin_log.h"
#include <linux/init.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/i2c.h>
#include <linux/delay.h>
#include <linux/videodev2.h>
#include <linux/clk.h>
#include <media/v4l2-device.h>
#include <media/v4l2-mediabus.h>
#include <linux/io.h>

#include "camera.h"
#include "sensor_helper.h"

MODULE_AUTHOR("hzh");
MODULE_DESCRIPTION("A low-level driver for C2398 sensors");
MODULE_LICENSE("GPL");

#define MCLK              (24*1000*1000)
#define V4L2_IDENT_SENSOR  0x2398

//define the registers

/*
 * Our nominal (default) frame rate.
 */

#define SENSOR_FRAME_RATE 20

/*
 * The GC2053 i2c address
 */
#define I2C_ADDR 0x6d

#define SENSOR_NUM 0x2
#define SENSOR_NAME "C2398_mipi"
#define SENSOR_NAME_2 "C2398_mipi_2"
#define SENSOR_MAXGAIN     (32<<4)


/*
 * The default register settings
 */

static struct regval_list sensor_default_regs[] = {

};


static struct regval_list sensor_1080p30_regs[] = {
	/* 0x3288,0x50 tm */
	{0x0401, 0x3b},
	{0x0403, 0x00},
	{0x3180, 0x20},
	{0x3c01, 0x13},
	{0x022d, 0x1f},
	{0x3087, 0x70},
	{0x3f08, 0x10},
	{0x3026, 0x01},
	{0x3009, 0x05},
	{0x0101, 0x01},
	/* pll */
	{0x0304, 0x05},/* 05: 30fps, 00: 60fps */
	{0x3517, 0x58},

	{0x3600, 0xd8},
	{0x3602, 0x01},
	{0x3583, 0x10},
	{0x3584, 0x02},
	{0xa000, 0x0b},
	{0xa001, 0x50},
	{0xa002, 0x00},
	{0xa003, 0x80},
	{0xa004, 0x00},
	{0xa005, 0x00},
	{0xa006, 0x00},
	{0xa007, 0x80},
	{0xa008, 0xce},
	{0xa009, 0x81},
	{0xa00a, 0x03},
	{0xa00b, 0x1c},
	{0xa00c, 0x03},
	{0xa00d, 0xa5},
	{0xa00e, 0x8c},
	{0xa00f, 0x81},
	{0xa010, 0x0a},
	{0xa011, 0x90},
	{0xa012, 0x8d},
	{0xa013, 0x81},
	{0xa014, 0x8c},
	{0xa015, 0x50},
	{0xa016, 0xbc},
	{0xa017, 0xd0},
	{0xa018, 0x92},
	{0xa019, 0xe0},
	{0xa01a, 0x85},
	{0xa01b, 0x01},
	{0xa01c, 0x84},
	{0xa01d, 0x60},
	{0xa01e, 0x06},
	{0xa01f, 0x81},
	{0xa020, 0x08},
	{0xa021, 0x10},
	{0xa022, 0x0f},
	{0xa023, 0x81},
	{0xa024, 0x88},
	{0xa025, 0x10},
	{0xa026, 0x0f},
	{0xa027, 0x01},
	{0xa028, 0x8f},
	{0xa029, 0x90},
	{0xa02a, 0xa4},
	{0xa02b, 0x7d},
	{0xa02c, 0x0e},
	{0xa02d, 0x81},
	{0xa02e, 0x5c},
	{0xa02f, 0xe0},
	{0xa030, 0x8f},
	{0xa031, 0x84},
	{0xa032, 0x83},
	{0xa033, 0x38},
	{0xa034, 0x0e},
	{0xa035, 0x03},
	{0xa036, 0xf1},
	{0xa037, 0x60},
	{0xa038, 0x8f},
	{0xa039, 0x84},
	{0xa03a, 0x03},
	{0xa03b, 0x38},
	{0xa03c, 0xa2},
	{0xa03d, 0x50},
	{0xa03e, 0x94},
	{0xa03f, 0x60},
	{0xa040, 0x0e},
	{0xa041, 0x81},
	{0xa042, 0x03},
	{0xa043, 0xa0},
	{0xa044, 0x8e},
	{0xa045, 0x99},
	{0xa046, 0x68},
	{0xa047, 0x61},
	{0xa048, 0x8e},
	{0xa049, 0x0f},
	{0xa04a, 0x92},
	{0xa04b, 0x60},
	{0xa04c, 0x05},
	{0xa04d, 0x01},
	{0xa04e, 0xb1},
	{0xa04f, 0xe0},
	{0xa050, 0x06},
	{0xa051, 0x81},
	{0xa052, 0x8e},
	{0xa053, 0x90},
	{0xa054, 0x08},
	{0xa055, 0x81},
	{0xa056, 0x90},
	{0xa057, 0xe0},
	{0xa058, 0x05},
	{0xa059, 0x81},
	{0xa05a, 0xb6},
	{0xa05b, 0x60},
	{0xa05c, 0x06},
	{0xa05d, 0x81},
	{0xa05e, 0x08},
	{0xa05f, 0x03},
	{0xa060, 0x0d},
	{0xa061, 0x10},
	{0xa062, 0x8a},
	{0xa063, 0x01},
	{0xa064, 0x0c},
	{0xa065, 0x9c},
	{0xa066, 0x03},
	{0xa067, 0x81},
	{0xa068, 0xce},
	{0xa069, 0x1d},
	{0xa06a, 0xce},
	{0xa06b, 0x9c},
	{0xa06c, 0x89},
	{0xa06d, 0x00},
	{0xa06e, 0x83},
	{0xa06f, 0x2d},
	{0xa070, 0x01},
	{0xa071, 0x2e},
	{0xa072, 0x8b},
	{0xa073, 0xac},
	{0xa074, 0x0b},
	{0xa075, 0x2f},
	{0xa076, 0x3b},
	{0xa077, 0x50},
	{0xa078, 0x83},
	{0xa079, 0x83},
	{0xa07a, 0xb7},
	{0xa07b, 0x50},
	{0x3583, 0x00},
	{0x3584, 0x22},
	{0x0100, 0x01}, /* stream on */
	/* [blc off] */
	/* 0x3108,0x00 */
};

static struct regval_list sensor_1080p25_regs[] = {
	/* 0x3288,0x50 tm */
	{0x0401, 0x3b},
	{0x0403, 0x00},
	{0x3180, 0x20},
	{0x3c01, 0x13},
	{0x022d, 0x1f},
	{0x3087, 0x70},
	{0x3f08, 0x10},
	{0x3026, 0x01},
	{0x3009, 0x05},
	{0x0101, 0x01},
	{0x0342, 0x0a},/* HTS */
	{0x0343, 0xab},
	/* pll */
	{0x0304, 0x05},/* 05: 30fps, 00: 60fps */
	{0x3517, 0x58},
	{0x3600, 0xd8},
	{0x3602, 0x01},
	{0x3583, 0x10},
	{0x3584, 0x02},
	{0xa000, 0x0b},
	{0xa001, 0x50},
	{0xa002, 0x00},
	{0xa003, 0x80},
	{0xa004, 0x00},
	{0xa005, 0x00},
	{0xa006, 0x00},
	{0xa007, 0x80},
	{0xa008, 0xce},
	{0xa009, 0x81},
	{0xa00a, 0x03},
	{0xa00b, 0x1c},
	{0xa00c, 0x03},
	{0xa00d, 0xa5},
	{0xa00e, 0x8c},
	{0xa00f, 0x81},
	{0xa010, 0x0a},
	{0xa011, 0x90},
	{0xa012, 0x8d},
	{0xa013, 0x81},
	{0xa014, 0x8c},
	{0xa015, 0x50},
	{0xa016, 0xbc},
	{0xa017, 0xd0},
	{0xa018, 0x92},
	{0xa019, 0xe0},
	{0xa01a, 0x85},
	{0xa01b, 0x01},
	{0xa01c, 0x84},
	{0xa01d, 0x60},
	{0xa01e, 0x06},
	{0xa01f, 0x81},
	{0xa020, 0x08},
	{0xa021, 0x10},
	{0xa022, 0x0f},
	{0xa023, 0x81},
	{0xa024, 0x88},
	{0xa025, 0x10},
	{0xa026, 0x0f},
	{0xa027, 0x01},
	{0xa028, 0x8f},
	{0xa029, 0x90},
	{0xa02a, 0xa4},
	{0xa02b, 0x7d},
	{0xa02c, 0x0e},
	{0xa02d, 0x81},
	{0xa02e, 0x5c},
	{0xa02f, 0xe0},
	{0xa030, 0x8f},
	{0xa031, 0x84},
	{0xa032, 0x83},
	{0xa033, 0x38},
	{0xa034, 0x0e},
	{0xa035, 0x03},
	{0xa036, 0xf1},
	{0xa037, 0x60},
	{0xa038, 0x8f},
	{0xa039, 0x84},
	{0xa03a, 0x03},
	{0xa03b, 0x38},
	{0xa03c, 0xa2},
	{0xa03d, 0x50},
	{0xa03e, 0x94},
	{0xa03f, 0x60},
	{0xa040, 0x0e},
	{0xa041, 0x81},
	{0xa042, 0x03},
	{0xa043, 0xa0},
	{0xa044, 0x8e},
	{0xa045, 0x99},
	{0xa046, 0x68},
	{0xa047, 0x61},
	{0xa048, 0x8e},
	{0xa049, 0x0f},
	{0xa04a, 0x92},
	{0xa04b, 0x60},
	{0xa04c, 0x05},
	{0xa04d, 0x01},
	{0xa04e, 0xb1},
	{0xa04f, 0xe0},
	{0xa050, 0x06},
	{0xa051, 0x81},
	{0xa052, 0x8e},
	{0xa053, 0x90},
	{0xa054, 0x08},
	{0xa055, 0x81},
	{0xa056, 0x90},
	{0xa057, 0xe0},
	{0xa058, 0x05},
	{0xa059, 0x81},
	{0xa05a, 0xb6},
	{0xa05b, 0x60},
	{0xa05c, 0x06},
	{0xa05d, 0x81},
	{0xa05e, 0x08},
	{0xa05f, 0x03},
	{0xa060, 0x0d},
	{0xa061, 0x10},
	{0xa062, 0x8a},
	{0xa063, 0x01},
	{0xa064, 0x0c},
	{0xa065, 0x9c},
	{0xa066, 0x03},
	{0xa067, 0x81},
	{0xa068, 0xce},
	{0xa069, 0x1d},
	{0xa06a, 0xce},
	{0xa06b, 0x9c},
	{0xa06c, 0x89},
	{0xa06d, 0x00},
	{0xa06e, 0x83},
	{0xa06f, 0x2d},
	{0xa070, 0x01},
	{0xa071, 0x2e},
	{0xa072, 0x8b},
	{0xa073, 0xac},
	{0xa074, 0x0b},
	{0xa075, 0x2f},
	{0xa076, 0x3b},
	{0xa077, 0x50},
	{0xa078, 0x83},
	{0xa079, 0x83},
	{0xa07a, 0xb7},
	{0xa07b, 0x50},
	{0x3583, 0x00},
	{0x3584, 0x22},
	{0x0100, 0x01}, /* stream on */
	/* [blc off] */
	/* 0x3108,0x00 */
};


static struct regval_list sensor_1080p30wdr_regs[] = {
	/* 0x3288,0x50 tm */
	{0x0401, 0x3b},
	{0x0403, 0x00},
	{0x3180, 0x20},
	{0x3c01, 0x13},
	{0x022d, 0x1f},
	{0x3087, 0x70},
	{0x3f08, 0x10},
	{0x3026, 0x01},
	{0x3009, 0x05},
	{0x0101, 0x01},

/* pll */
	{0x0304, 0x05},/* 05: 30fps, 00: 60fps */
	{0x3517, 0x58},
	{0x0304, 0x00},
	{0x3211, 0x3b},
	{0x3216, 0x28},
	{0x3217, 0x28},
	{0x3219, 0x52},
	{0x3226, 0x00},
	{0x3227, 0x80},
	{0x3089, 0x40},
	{0x31a0, 0x20},
	{0x3218, 0x4c},
	{0x322b, 0x06},
	{0x3c01, 0x33},
	{0x330a, 0x10},
	{0x330b, 0x10},
	{0x330c, 0x10},
	{0x330d, 0x10},
/* 	{0x323b,0x23},
	{0x3023,0x06},
	{0x3181,0xd2},/* 52
	{0x3186, 0x03},/* 01
	{0x380c, 0x20},
	{0x380d, 0x10},
	{0x380f, 0x08},
	{0x3817, 0x10}, */

	{0x323b, 0x21},
	{0x3023, 0x06},
	{0x3181, 0xd2},/* 52 */
	{0x3186, 0x03},/* 01 */
	{0x380c, 0x20},
	{0x380d, 0x10},
	{0x380f, 0x08},
	{0x3817, 0x10},
	{0x0202, 0x04},
	{0x0203, 0x0e},
	{0x308a, 0x00},
	{0x308b, 0x40},
	/* 0x3907,0x */

	{0x3600, 0xc8},
	{0x3602, 0x08},
	{0x3583, 0x10},
	{0x3584, 0x02},
	{0xa000, 0x8b},
	{0xa001, 0xd0},
	{0xa002, 0x00},
	{0xa003, 0x80},
	{0xa004, 0x80},
	{0xa005, 0x00},
	{0xa006, 0x80},
	{0xa007, 0x00},
	{0xa008, 0xce},
	{0xa009, 0x01},
	{0xa00a, 0x03},
	{0xa00b, 0x1c},
	{0xa00c, 0x83},
	{0xa00d, 0xa5},
	{0xa00e, 0x8c},
	{0xa00f, 0x01},
	{0xa010, 0x0a},
	{0xa011, 0x10},
	{0xa012, 0x8d},
	{0xa013, 0x81},
	{0xa014, 0x8c},
	{0xa015, 0x50},
	{0xa016, 0x85},
	{0xa017, 0xd1},
	{0xa018, 0x03},
	{0xa019, 0x60},
	{0xa01a, 0x94},
	{0xa01b, 0x15},
	{0xa01c, 0x14},
	{0xa01d, 0x8b},
	{0xa01e, 0x30},
	{0xa01f, 0x60},
	{0xa020, 0x85},
	{0xa021, 0x81},
	{0xa022, 0x06},
	{0xa023, 0x01},
	{0xa024, 0x08},
	{0xa025, 0x10},
	{0xa026, 0x12},
	{0xa027, 0x81},
	{0xa028, 0x08},
	{0xa029, 0x90},
	{0xa02a, 0x12},
	{0xa02b, 0x01},
	{0xa02c, 0x92},
	{0xa02d, 0x91},
	{0xa02e, 0x83},
	{0xa02f, 0xb2},
	{0xa030, 0x14},
	{0xa031, 0x03},
	{0xa032, 0x14},
	{0xa033, 0x10},
	{0xa034, 0x03},
	{0xa035, 0x32},
	{0xa036, 0xa0},
	{0xa037, 0x50},
	{0xa038, 0x14},
	{0xa039, 0x90},
	{0xa03a, 0x83},
	{0xa03b, 0xf4},
	{0xa03c, 0x83},
	{0xa03d, 0x3a},
	{0xa03e, 0x26},
	{0xa03f, 0xd0},
	{0xa040, 0xa2},
	{0xa041, 0xe0},
	{0xa042, 0x05},
	{0xa043, 0x01},
	{0xa044, 0x32},
	{0xa045, 0x60},
	{0xa046, 0x86},
	{0xa047, 0x81},
	{0xa048, 0x82},
	{0xa049, 0x61},
	{0xa04a, 0xab},
	{0xa04b, 0xd0},
	{0xa04c, 0x22},
	{0xa04d, 0x60},
	{0xa04e, 0x85},
	{0xa04f, 0x81},
	{0xa050, 0x32},
	{0xa051, 0xe0},
	{0xa052, 0x06},
	{0xa053, 0x01},
	{0xa054, 0x82},
	{0xa055, 0x60},
	{0xa056, 0x08},
	{0xa057, 0x01},
	{0xa058, 0xb1},
	{0xa059, 0x60},
	{0xa05a, 0x85},
	{0xa05b, 0x01},
	{0xa05c, 0xb0},
	{0xa05d, 0x60},
	{0xa05e, 0x86},
	{0xa05f, 0x01},
	{0xa060, 0x88},
	{0xa061, 0x90},
	{0xa062, 0x0e},
	{0xa063, 0x81},
	{0xa064, 0x88},
	{0xa065, 0x10},
	{0xa066, 0x0e},
	{0xa067, 0x01},
	{0xa068, 0x12},
	{0xa069, 0x96},
	{0xa06a, 0x74},
	{0xa06b, 0x50},
	{0xa06c, 0x8e},
	{0xa06d, 0x91},
	{0xa06e, 0x03},
	{0xa06f, 0x3a},
	{0xa070, 0x74},
	{0xa071, 0xd0},
	{0xa072, 0x8a},
	{0xa073, 0x61},
	{0xa074, 0x05},
	{0xa075, 0x81},
	{0xa076, 0x30},
	{0xa077, 0xe0},
	{0xa078, 0x86},
	{0xa079, 0x01},
	{0xa07a, 0x08},
	{0xa07b, 0x10},
	{0xa07c, 0x0f},
	{0xa07d, 0x01},
	{0xa07e, 0x88},
	{0xa07f, 0x10},
	{0xa080, 0x8f},
	{0xa081, 0x01},
	{0xa082, 0x8b},
	{0xa083, 0xe1},
	{0xa084, 0x85},
	{0xa085, 0x01},
	{0xa086, 0xb0},
	{0xa087, 0xe0},
	{0xa088, 0x06},
	{0xa089, 0x81},
	{0xa08a, 0x08},
	{0xa08b, 0x90},
	{0xa08c, 0x10},
	{0xa08d, 0x01},
	{0xa08e, 0x88},
	{0xa08f, 0x90},
	{0xa090, 0x90},
	{0xa091, 0x81},
	{0xa092, 0x20},
	{0xa093, 0xe0},
	{0xa094, 0x85},
	{0xa095, 0x81},
	{0xa096, 0x82},
	{0xa097, 0xe0},
	{0xa098, 0x86},
	{0xa099, 0x81},
	{0xa09a, 0x8f},
	{0xa09b, 0x90},
	{0xa09c, 0x88},
	{0xa09d, 0x01},
	{0xa09e, 0xa1},
	{0xa09f, 0x60},
	{0xa0a0, 0x85},
	{0xa0a1, 0x81},
	{0xa0a2, 0x82},
	{0xa0a3, 0xe0},
	{0xa0a4, 0x86},
	{0xa0a5, 0x81},
	{0xa0a6, 0x90},
	{0xa0a7, 0x90},
	{0xa0a8, 0x88},
	{0xa0a9, 0x81},
	{0xa0aa, 0x12},
	{0xa0ab, 0xe0},
	{0xa0ac, 0x85},
	{0xa0ad, 0x01},
	{0xa0ae, 0x04},
	{0xa0af, 0x60},
	{0xa0b0, 0x06},
	{0xa0b1, 0x81},
	{0xa0b2, 0x88},
	{0xa0b3, 0x10},
	{0xa0b4, 0x13},
	{0xa0b5, 0x81},
	{0xa0b6, 0x88},
	{0xa0b7, 0x90},
	{0xa0b8, 0x13},
	{0xa0b9, 0x81},
	{0xa0ba, 0x93},
	{0xa0bb, 0x90},
	{0xa0bc, 0x24},
	{0xa0bd, 0xfd},
	{0xa0be, 0x11},
	{0xa0bf, 0x81},
	{0xa0c0, 0xdc},
	{0xa0c1, 0x60},
	{0xa0c2, 0x93},
	{0xa0c3, 0x04},
	{0xa0c4, 0x03},
	{0xa0c5, 0x38},
	{0xa0c6, 0x91},
	{0xa0c7, 0x03},
	{0xa0c8, 0xf1},
	{0xa0c9, 0x60},
	{0xa0ca, 0x93},
	{0xa0cb, 0x04},
	{0xa0cc, 0x83},
	{0xa0cd, 0xb8},
	{0xa0ce, 0xeb},
	{0xa0cf, 0x50},
	{0xa0d0, 0x94},
	{0xa0d1, 0x60},
	{0xa0d2, 0x11},
	{0xa0d3, 0x81},
	{0xa0d4, 0x03},
	{0xa0d5, 0x20},
	{0xa0d6, 0x91},
	{0xa0d7, 0x19},
	{0xa0d8, 0xe8},
	{0xa0d9, 0x61},
	{0xa0da, 0x91},
	{0xa0db, 0x0f},
	{0xa0dc, 0x92},
	{0xa0dd, 0xe0},
	{0xa0de, 0x85},
	{0xa0df, 0x81},
	{0xa0e0, 0xb1},
	{0xa0e1, 0xe0},
	{0xa0e2, 0x06},
	{0xa0e3, 0x01},
	{0xa0e4, 0x11},
	{0xa0e5, 0x90},
	{0xa0e6, 0x88},
	{0xa0e7, 0x81},
	{0xa0e8, 0x90},
	{0xa0e9, 0xe0},
	{0xa0ea, 0x05},
	{0xa0eb, 0x81},
	{0xa0ec, 0xb6},
	{0xa0ed, 0x60},
	{0xa0ee, 0x06},
	{0xa0ef, 0x81},
	{0xa0f0, 0x08},
	{0xa0f1, 0x83},
	{0xa0f2, 0x8d},
	{0xa0f3, 0x90},
	{0xa0f4, 0x8a},
	{0xa0f5, 0x81},
	{0xa0f6, 0x0c},
	{0xa0f7, 0x9c},
	{0xa0f8, 0x03},
	{0xa0f9, 0x01},
	{0xa0fa, 0xce},
	{0xa0fb, 0x1d},
	{0xa0fc, 0xce},
	{0xa0fd, 0x1c},
	{0xa0fe, 0x09},
	{0xa0ff, 0x00},
	{0xa100, 0x83},
	{0xa101, 0xad},
	{0xa102, 0x01},
	{0xa103, 0x2e},
	{0xa104, 0x0b},
	{0xa105, 0x2c},
	{0xa106, 0x0b},
	{0xa107, 0x2f},
	{0xa108, 0x04},
	{0xa109, 0x51},
	{0xa10a, 0x14},
	{0xa10b, 0x83},
	{0xa10c, 0x83},
	{0xa10d, 0x83},
	{0xa10e, 0x80},
	{0xa10f, 0xd1},
	{0x3583, 0x00},
	{0x3584, 0x22},
	{0x0100, 0x01},/* stream on */

/* [blc off] */
/* 0x3108,0x00 */
/* 0x3308,0x00 */
};

static struct regval_list sensor_1080p25wdr_regs[] = {
	/* 0x3288,0x50 tm */
	{0x0401, 0x3b},
	{0x0403, 0x00},
	{0x3180, 0x20},
	{0x3c01, 0x13},
	{0x022d, 0x1f},
	{0x3087, 0x70},
	{0x3f08, 0x10},
	{0x3026, 0x01},
	{0x3009, 0x05},
	{0x0101, 0x01},
	{0x0342, 0x0a},/* HTS */
	{0x0343, 0xab},

/* pll */
	{0x0304, 0x05},/* 05: 30fps, 00: 60fps */
	{0x3517, 0x58},
	{0x0304, 0x00},
	{0x3211, 0x3b},
	{0x3216, 0x28},
	{0x3217, 0x28},
	{0x3219, 0x52},
	{0x3226, 0x00},
	{0x3227, 0x80},
	{0x3089, 0x40},
	{0x31a0, 0x20},
	{0x3218, 0x4c},
	{0x322b, 0x06},
	{0x3c01, 0x33},
	{0x330a, 0x10},
	{0x330b, 0x10},
	{0x330c, 0x10},
	{0x330d, 0x10},
	/* {0x323b,0x23},
	{0x3023,0x06},
	{0x3181,0xd2},/* 52
	{0x3186,0x03},/* 01
	{0x380c,0x20},
	{0x380d,0x10},
	{0x380f,0x08},
	{0x3817,0x10}, */

	{0x323b, 0x21},
	{0x3023, 0x06},
	{0x3181, 0xd2},/* 52 */
	{0x3186, 0x03},/* 01 */
	{0x380c, 0x20},
	{0x380d, 0x10},
	{0x380f, 0x08},
	{0x3817, 0x10},


	{0x0202, 0x04},
	{0x0203, 0x0e},
	{0x308a, 0x00},
	{0x308b, 0x40},
	/* 0x3907,0x */

	{0x3600, 0xc8},
	{0x3602, 0x08},
	{0x3583, 0x10},
	{0x3584, 0x02},
	{0xa000, 0x8b},
	{0xa001, 0xd0},
	{0xa002, 0x00},
	{0xa003, 0x80},
	{0xa004, 0x80},
	{0xa005, 0x00},
	{0xa006, 0x80},
	{0xa007, 0x00},
	{0xa008, 0xce},
	{0xa009, 0x01},
	{0xa00a, 0x03},
	{0xa00b, 0x1c},
	{0xa00c, 0x83},
	{0xa00d, 0xa5},
	{0xa00e, 0x8c},
	{0xa00f, 0x01},
	{0xa010, 0x0a},
	{0xa011, 0x10},
	{0xa012, 0x8d},
	{0xa013, 0x81},
	{0xa014, 0x8c},
	{0xa015, 0x50},
	{0xa016, 0x85},
	{0xa017, 0xd1},
	{0xa018, 0x03},
	{0xa019, 0x60},
	{0xa01a, 0x94},
	{0xa01b, 0x15},
	{0xa01c, 0x14},
	{0xa01d, 0x8b},
	{0xa01e, 0x30},
	{0xa01f, 0x60},
	{0xa020, 0x85},
	{0xa021, 0x81},
	{0xa022, 0x06},
	{0xa023, 0x01},
	{0xa024, 0x08},
	{0xa025, 0x10},
	{0xa026, 0x12},
	{0xa027, 0x81},
	{0xa028, 0x08},
	{0xa029, 0x90},
	{0xa02a, 0x12},
	{0xa02b, 0x01},
	{0xa02c, 0x92},
	{0xa02d, 0x91},
	{0xa02e, 0x83},
	{0xa02f, 0xb2},
	{0xa030, 0x14},
	{0xa031, 0x03},
	{0xa032, 0x14},
	{0xa033, 0x10},
	{0xa034, 0x03},
	{0xa035, 0x32},
	{0xa036, 0xa0},
	{0xa037, 0x50},
	{0xa038, 0x14},
	{0xa039, 0x90},
	{0xa03a, 0x83},
	{0xa03b, 0xf4},
	{0xa03c, 0x83},
	{0xa03d, 0x3a},
	{0xa03e, 0x26},
	{0xa03f, 0xd0},
	{0xa040, 0xa2},
	{0xa041, 0xe0},
	{0xa042, 0x05},
	{0xa043, 0x01},
	{0xa044, 0x32},
	{0xa045, 0x60},
	{0xa046, 0x86},
	{0xa047, 0x81},
	{0xa048, 0x82},
	{0xa049, 0x61},
	{0xa04a, 0xab},
	{0xa04b, 0xd0},
	{0xa04c, 0x22},
	{0xa04d, 0x60},
	{0xa04e, 0x85},
	{0xa04f, 0x81},
	{0xa050, 0x32},
	{0xa051, 0xe0},
	{0xa052, 0x06},
	{0xa053, 0x01},
	{0xa054, 0x82},
	{0xa055, 0x60},
	{0xa056, 0x08},
	{0xa057, 0x01},
	{0xa058, 0xb1},
	{0xa059, 0x60},
	{0xa05a, 0x85},
	{0xa05b, 0x01},
	{0xa05c, 0xb0},
	{0xa05d, 0x60},
	{0xa05e, 0x86},
	{0xa05f, 0x01},
	{0xa060, 0x88},
	{0xa061, 0x90},
	{0xa062, 0x0e},
	{0xa063, 0x81},
	{0xa064, 0x88},
	{0xa065, 0x10},
	{0xa066, 0x0e},
	{0xa067, 0x01},
	{0xa068, 0x12},
	{0xa069, 0x96},
	{0xa06a, 0x74},
	{0xa06b, 0x50},
	{0xa06c, 0x8e},
	{0xa06d, 0x91},
	{0xa06e, 0x03},
	{0xa06f, 0x3a},
	{0xa070, 0x74},
	{0xa071, 0xd0},
	{0xa072, 0x8a},
	{0xa073, 0x61},
	{0xa074, 0x05},
	{0xa075, 0x81},
	{0xa076, 0x30},
	{0xa077, 0xe0},
	{0xa078, 0x86},
	{0xa079, 0x01},
	{0xa07a, 0x08},
	{0xa07b, 0x10},
	{0xa07c, 0x0f},
	{0xa07d, 0x01},
	{0xa07e, 0x88},
	{0xa07f, 0x10},
	{0xa080, 0x8f},
	{0xa081, 0x01},
	{0xa082, 0x8b},
	{0xa083, 0xe1},
	{0xa084, 0x85},
	{0xa085, 0x01},
	{0xa086, 0xb0},
	{0xa087, 0xe0},
	{0xa088, 0x06},
	{0xa089, 0x81},
	{0xa08a, 0x08},
	{0xa08b, 0x90},
	{0xa08c, 0x10},
	{0xa08d, 0x01},
	{0xa08e, 0x88},
	{0xa08f, 0x90},
	{0xa090, 0x90},
	{0xa091, 0x81},
	{0xa092, 0x20},
	{0xa093, 0xe0},
	{0xa094, 0x85},
	{0xa095, 0x81},
	{0xa096, 0x82},
	{0xa097, 0xe0},
	{0xa098, 0x86},
	{0xa099, 0x81},
	{0xa09a, 0x8f},
	{0xa09b, 0x90},
	{0xa09c, 0x88},
	{0xa09d, 0x01},
	{0xa09e, 0xa1},
	{0xa09f, 0x60},
	{0xa0a0, 0x85},
	{0xa0a1, 0x81},
	{0xa0a2, 0x82},
	{0xa0a3, 0xe0},
	{0xa0a4, 0x86},
	{0xa0a5, 0x81},
	{0xa0a6, 0x90},
	{0xa0a7, 0x90},
	{0xa0a8, 0x88},
	{0xa0a9, 0x81},
	{0xa0aa, 0x12},
	{0xa0ab, 0xe0},
	{0xa0ac, 0x85},
	{0xa0ad, 0x01},
	{0xa0ae, 0x04},
	{0xa0af, 0x60},
	{0xa0b0, 0x06},
	{0xa0b1, 0x81},
	{0xa0b2, 0x88},
	{0xa0b3, 0x10},
	{0xa0b4, 0x13},
	{0xa0b5, 0x81},
	{0xa0b6, 0x88},
	{0xa0b7, 0x90},
	{0xa0b8, 0x13},
	{0xa0b9, 0x81},
	{0xa0ba, 0x93},
	{0xa0bb, 0x90},
	{0xa0bc, 0x24},
	{0xa0bd, 0xfd},
	{0xa0be, 0x11},
	{0xa0bf, 0x81},
	{0xa0c0, 0xdc},
	{0xa0c1, 0x60},
	{0xa0c2, 0x93},
	{0xa0c3, 0x04},
	{0xa0c4, 0x03},
	{0xa0c5, 0x38},
	{0xa0c6, 0x91},
	{0xa0c7, 0x03},
	{0xa0c8, 0xf1},
	{0xa0c9, 0x60},
	{0xa0ca, 0x93},
	{0xa0cb, 0x04},
	{0xa0cc, 0x83},
	{0xa0cd, 0xb8},
	{0xa0ce, 0xeb},
	{0xa0cf, 0x50},
	{0xa0d0, 0x94},
	{0xa0d1, 0x60},
	{0xa0d2, 0x11},
	{0xa0d3, 0x81},
	{0xa0d4, 0x03},
	{0xa0d5, 0x20},
	{0xa0d6, 0x91},
	{0xa0d7, 0x19},
	{0xa0d8, 0xe8},
	{0xa0d9, 0x61},
	{0xa0da, 0x91},
	{0xa0db, 0x0f},
	{0xa0dc, 0x92},
	{0xa0dd, 0xe0},
	{0xa0de, 0x85},
	{0xa0df, 0x81},
	{0xa0e0, 0xb1},
	{0xa0e1, 0xe0},
	{0xa0e2, 0x06},
	{0xa0e3, 0x01},
	{0xa0e4, 0x11},
	{0xa0e5, 0x90},
	{0xa0e6, 0x88},
	{0xa0e7, 0x81},
	{0xa0e8, 0x90},
	{0xa0e9, 0xe0},
	{0xa0ea, 0x05},
	{0xa0eb, 0x81},
	{0xa0ec, 0xb6},
	{0xa0ed, 0x60},
	{0xa0ee, 0x06},
	{0xa0ef, 0x81},
	{0xa0f0, 0x08},
	{0xa0f1, 0x83},
	{0xa0f2, 0x8d},
	{0xa0f3, 0x90},
	{0xa0f4, 0x8a},
	{0xa0f5, 0x81},
	{0xa0f6, 0x0c},
	{0xa0f7, 0x9c},
	{0xa0f8, 0x03},
	{0xa0f9, 0x01},
	{0xa0fa, 0xce},
	{0xa0fb, 0x1d},
	{0xa0fc, 0xce},
	{0xa0fd, 0x1c},
	{0xa0fe, 0x09},
	{0xa0ff, 0x00},
	{0xa100, 0x83},
	{0xa101, 0xad},
	{0xa102, 0x01},
	{0xa103, 0x2e},
	{0xa104, 0x0b},
	{0xa105, 0x2c},
	{0xa106, 0x0b},
	{0xa107, 0x2f},
	{0xa108, 0x04},
	{0xa109, 0x51},
	{0xa10a, 0x14},
	{0xa10b, 0x83},
	{0xa10c, 0x83},
	{0xa10d, 0x83},
	{0xa10e, 0x80},
	{0xa10f, 0xd1},
	{0x3583, 0x00},
	{0x3584, 0x22},
	{0x0100, 0x01},/* stream on */

/* [blc off] */
/* 0x3108,0x00 */
/* 0x3308,0x00 */
};




/*
 * Here we'll try to encapsulate the changes for just the output
 * video format.
 *
 */

static struct regval_list sensor_fmt_raw[] = {

};


/*
 * Code for dealing with controls.
 * fill with different sensor module
 * different sensor module has different settings here
 * if not support the follow function , retrun -EINVAL
 */




static int sensor_g_exp(struct v4l2_subdev *sd, __s32 *value)
{
	struct sensor_info *info = to_state(sd);
	*value = info->exp;
	sensor_dbg("sensor_get_exposure = %d\n", info->exp);
	return 0;
}




static int sensor_s_exp(struct v4l2_subdev *sd, unsigned int exp_val)
{
	data_type explow, exphigh;
	struct sensor_info *info = to_state(sd);

	exphigh = (unsigned char) (0xff & (exp_val>>12));
	explow = (unsigned char) (0xff & (exp_val>>4));

	sensor_write(sd, 0x0203, explow);
	sensor_write(sd, 0x0202, exphigh);

	info->exp = exp_val;
	return 0;
}

static int sensor_g_gain(struct v4l2_subdev *sd, __s32 *value)
{
	struct sensor_info *info = to_state(sd);
	*value = info->gain;
	sensor_dbg("sensor_get_gain = %d\n", info->gain);
	return 0;
}

static int sensor_s_gain(struct v4l2_subdev *sd, int gain_val)
{

	data_type gainlow, gainhigh;

	struct sensor_info *info = to_state(sd);

	/* if (gain_val == info->gain) {
		return 0;
	}

	sensor_dbg("gain_val:%d\n", gain_val);
	setSensorGain(sd, gain_val * 4); */
		gainhigh = (unsigned char) (0xff & (gain_val>>8));
		gainlow = (unsigned char) (0xff & (gain_val));
		sensor_write(sd, 0x0205, gainlow);
		sensor_write(sd, 0x0204, gainhigh);

	info->gain = gain_val;
	return 0;
}


static int sensor_s_exp_gain(struct v4l2_subdev *sd,
				 struct sensor_exp_gain *exp_gain)
{
	int exp_val, gain_val;
	struct sensor_info *info = to_state(sd);

	exp_val = exp_gain->exp_val;
	gain_val = exp_gain->gain_val;

	if (gain_val < (1 * 16)) {
		gain_val = 16;
	}

	if (exp_val > 0xfffff)
		exp_val = 0xfffff;

	sensor_s_exp(sd, exp_val);
	sensor_s_gain(sd, gain_val);
	sensor_write(sd, 0x340f, 0x11);
	sensor_dbg("sensor_set_gain exp = %d, %d Done!\n", gain_val, exp_val);
	info->exp = exp_val;
	info->gain = gain_val;
	return 0;
}



static int sensor_s_sw_stby(struct v4l2_subdev *sd, int on_off)
{
	int ret;
	data_type rdval;
	ret = sensor_read(sd, 0x0100, &rdval);
	if (ret != 0)
		return ret;
	if (on_off == STBY_ON)
		ret = sensor_write(sd, 0x0100, rdval&0x00);
	else
		ret = sensor_write(sd, 0x0100, rdval|0x01);
	return ret;
}


static int sensor_get_fmt_mbus_core(struct v4l2_subdev *sd, int *code)
{
	struct sensor_info *info = to_state(sd);
	data_type get_value;
	sensor_read(sd, 0x0101, &get_value);
	switch (get_value) {
	case 0x00:
		*code = MEDIA_BUS_FMT_SGBRG10_1X10;
		break;
	case 0x01:
		*code = MEDIA_BUS_FMT_SBGGR10_1X10;
		break;
	case 0x02:
		*code = MEDIA_BUS_FMT_SRGGB10_1X10;
		break;
	case 0x03:
		*code = MEDIA_BUS_FMT_SGRBG10_1X10;
		break;
	default:
		*code = info->fmt->mbus_code;
	}
	return 0;
}

data_type sensor_flip_status;

static int sensor_s_hflip(struct v4l2_subdev *sd, int enable)
{
	 data_type get_value;
	 data_type set_value = 0;

	 if (!(enable == 0 || enable == 1))
		return -1;

	 /* sensor_read(sd, 0x0101, &get_value); */
	 get_value = sensor_flip_status;
	 /* printk("--sensor hfilp set[%d] read value:0x%X --\n", enable, get_value); */
	 if (enable)
		 set_value = get_value ^ 0x01;
	 sensor_flip_status = set_value;
	 /* printk("--set sensor hfilp the value:0x%X \n--", set_value); */
	 sensor_write(sd, 0x0101, set_value);
	 return 0;
}
static int sensor_s_vflip(struct v4l2_subdev *sd, int enable)
{
	 data_type get_value;
	 data_type set_value = 0;
	 if (!(enable == 0 || enable == 1))
		 return -1;
	 /* sensor_read(sd, 0x17, &get_value); */
	 get_value = sensor_flip_status;
	 /* printk("--sensor vfilp set[%d] read value:0x%X --\n", enable, get_value); */
	 if (enable)
		 set_value = get_value ^ 0x02;
	 sensor_flip_status = set_value;
	 /* printk("--set sensor vfilp the value:0x%X --\n", set_value); */
	 sensor_write(sd, 0x0101, set_value);
	 return 0;
}


/*
 * Stuff that knows about the sensor.
 */
static int sensor_power(struct v4l2_subdev *sd, int on)
{
	int ret = 0;

	switch (on) {
	case STBY_ON:
		sensor_dbg("STBY_ON!\n");
		cci_lock(sd);
		ret = sensor_s_sw_stby(sd, STBY_ON);
		if (ret < 0)
			sensor_err("soft stby falied!\n");
		usleep_range(10000, 12000);
		cci_unlock(sd);
		break;
	case STBY_OFF:
		sensor_dbg("STBY_OFF!\n");
		cci_lock(sd);
		usleep_range(10000, 12000);
		ret = sensor_s_sw_stby(sd, STBY_OFF);
		if (ret < 0)
			sensor_err("soft stby off falied!\n");
		cci_unlock(sd);
		break;
	case PWR_ON:
		sensor_dbg("PWR_ON!\n");
		cci_lock(sd);
		vin_gpio_set_status(sd, PWDN, 1);
		vin_gpio_set_status(sd, RESET, 1);
		vin_gpio_set_status(sd, POWER_EN, 1);
		/* vin_gpio_write(sd, RESET, CSI_GPIO_HIGH); */
		vin_gpio_write(sd, RESET, CSI_GPIO_LOW);
		vin_gpio_write(sd, PWDN, CSI_GPIO_LOW);
		/* vin_gpio_write(sd, POWER_EN, CSI_GPIO_HIGH); */
		vin_set_pmu_channel(sd, IOVDD, ON);
		vin_set_pmu_channel(sd, DVDD, ON);
		vin_set_pmu_channel(sd, AVDD, ON);

		usleep_range(10000, 12000);
		vin_gpio_write(sd, RESET, CSI_GPIO_HIGH);
		vin_gpio_write(sd, PWDN, CSI_GPIO_HIGH);
		usleep_range(10000, 12000);
		vin_set_mclk(sd, ON);
		usleep_range(10000, 12000);
		vin_set_mclk_freq(sd, MCLK);
		usleep_range(8000, 10000);
		cci_unlock(sd);
		break;
	case PWR_OFF:
		sensor_dbg("PWR_OFF!\n");
		cci_lock(sd);
		/* vin_gpio_set_status(sd, PWDN, 1); */
		vin_gpio_set_status(sd, RESET, 1);
		vin_gpio_write(sd, RESET, CSI_GPIO_LOW);
		/* vin_gpio_write(sd, PWDN, CSI_GPIO_LOW); */
		vin_set_mclk(sd, OFF);
		vin_set_pmu_channel(sd, AFVDD, OFF);
		vin_set_pmu_channel(sd, AVDD, OFF);
		vin_set_pmu_channel(sd, IOVDD, OFF);
		vin_set_pmu_channel(sd, DVDD, OFF);
		vin_gpio_write(sd, POWER_EN, CSI_GPIO_LOW);
		vin_gpio_set_status(sd, RESET, 0);
		/* vin_gpio_set_status(sd, PWDN, 0); */
		vin_gpio_set_status(sd, POWER_EN, 0);
		cci_unlock(sd);
		break;

	default:
		return -EINVAL;
	}

	return 0;
}

static int sensor_reset(struct v4l2_subdev *sd, u32 val)
{

	switch (val) {
	case 0:
		vin_gpio_write(sd, RESET, CSI_GPIO_HIGH);
		usleep_range(1000, 1200);
		break;

	case 1:
		vin_gpio_write(sd, RESET, CSI_GPIO_LOW);
		usleep_range(1000, 1200);
		break;

	default:
		return -EINVAL;
	}

	return 0;
}

static int sensor_detect(struct v4l2_subdev *sd)
{
	unsigned int SENSOR_ID = 0;
	data_type rdval;
	int cnt = 0;
	sensor_print("%s: start\n", __func__);

	sensor_read(sd, 0x0000, &rdval);
	SENSOR_ID |= (rdval << 8);
	sensor_read(sd, 0x0001, &rdval);
	SENSOR_ID |= (rdval);
	sensor_print("V4L2_IDENT_SENSOR = 0x%x\n", SENSOR_ID);

	while ((SENSOR_ID != V4L2_IDENT_SENSOR) && (cnt < 3)) {
		sensor_read(sd, 0x0000, &rdval);
		SENSOR_ID |= (rdval << 8);
		sensor_read(sd, 0x0001, &rdval);
		SENSOR_ID |= (rdval);
		sensor_print("retry = %d, V4L2_IDENT_SENSOR = %x\n",
			cnt, SENSOR_ID);
		cnt++;
		}
	sensor_read(sd, 0x3109, &rdval);
	sensor_print("0x3109 == 0x%x\n", rdval);
	if (SENSOR_ID != V4L2_IDENT_SENSOR)
		return -ENODEV;
	return 0;
}

static int sensor_init(struct v4l2_subdev *sd, u32 val)
{
	int ret;
	struct sensor_info *info = to_state(sd);
	sensor_print("sensor_init, start sensor_detect\n");
	/* Make sure it is a target sensor */
	ret = sensor_detect(sd);
	if (ret) {
		sensor_err("chip found is not an target chip.\n");
		return ret;
	}
	info->focus_status = 0;
	info->low_speed    = 0;
	info->width        = 1920;
	info->height       = 1080;
	info->hflip        = 0;
	info->vflip        = 0;
	info->gain         = 0;
	info->exp          = 0;
	info->tpf.numerator      = 1;
	info->tpf.denominator    = 30;/* 30fps */
	info->preview_first_flag = 1;
	return 0;
}

static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
{
	int ret = 0;
	struct sensor_info *info = to_state(sd);

	switch (cmd) {
	case GET_CURRENT_WIN_CFG:
		/* sensor_err("%s: GET_CURRENT_WIN_CFG, info->current_wins=%p\n", __func__, info->current_wins); */
		if (info->current_wins != NULL) {
			memcpy(arg, info->current_wins,
				sizeof(*info->current_wins));
			ret = 0;
		} else {
			sensor_err("empty wins!\n");
			ret = -1;
		}
		break;
	case SET_FPS:
		break;
	case VIDIOC_VIN_SENSOR_EXP_GAIN:
		sensor_s_exp_gain(sd, (struct sensor_exp_gain *)arg);
		break;
	/* case VIDIOC_VIN_SENSOR_SET_FPS:
		ret = sensor_s_fps(sd, (struct sensor_fps *)arg);
		break; */
	case VIDIOC_VIN_SENSOR_CFG_REQ:
		sensor_cfg_req(sd, (struct sensor_config *)arg);
		break;
	case VIDIOC_VIN_GET_SENSOR_CODE:
		sensor_get_fmt_mbus_core(sd, (int *)arg);
		break;
	default:
		return -EINVAL;
	}
	return ret;
}

/*
 * Store information about the video data format.
 */
static struct sensor_format_struct sensor_formats[] = {
	{
		.desc      = "Raw RGB Bayer",
		.mbus_code = MEDIA_BUS_FMT_SBGGR10_1X10, /* .mbus_code = MEDIA_BUS_FMT_SBGGR10_1X10, */
		.regs      = sensor_fmt_raw,
		.regs_size = ARRAY_SIZE(sensor_fmt_raw),
		.bpp       = 1
	},
};
#define N_FMTS ARRAY_SIZE(sensor_formats)

/*
 * Then there is the issue of window sizes.  Try to capture the info here.
 */

static struct sensor_win_size sensor_win_sizes[] = {
#if 1
	{
		.width      = 1920,
		.height     = 1080,
		.hoffset    = 0,
		.voffset    = 0,
		.hts        = 2412,
		.vts        = 1104,
		.pclk       = 79885440,
		.mipi_bps   = 176 * 1000 * 1000,
		.fps_fixed  = 30,
		.bin_factor = 1,
		.intg_min   = 1 << 4,
		.intg_max   = 1284 << 4,
		.gain_min   = 1 << 4,
		.gain_max   = 110 << 4,
		.regs       = sensor_1080p30_regs,
		.regs_size  = ARRAY_SIZE(sensor_1080p30_regs),
		.set_size   = NULL,
	},
	{
		.width      = 1920,
		.height     = 1080,
		.hoffset    = 0,
		.voffset    = 0,
		.hts        = 2731,
		.vts        = 1104,
		.pclk       = 79885440,
		.mipi_bps   = 176 * 1000 * 1000,
		.fps_fixed  = 25,
		.bin_factor = 1,
		.intg_min   = 1 << 4,
		.intg_max   = 1284 << 4,
		.gain_min   = 1 << 4,
		.gain_max   = 110 << 4,
		.regs       = sensor_1080p25_regs,
		.regs_size  = ARRAY_SIZE(sensor_1080p25_regs),
		.set_size   = NULL,
	},
#endif
/*
	 {
	 .width = 1920,
	 .height = 1080,
	 .hoffset = 0,
	 .voffset = 0,
	 .hts = 2894,
	 .vts = 1104,
	 .pclk = 159770880,
	 .mipi_bps = 176 * 1000 * 1000,
	 .fps_fixed = 30,
	 .bin_factor = 1,
	 .if_mode = MIPI_VC_WDR_MODE,
	 /* .if_mode = MIPI_DOL_WDR_MODE,
	 .wdr_mode = ISP_DOL_WDR_MODE,
	 .intg_min = 1 << 4,
	 .intg_max = (1284 - 10) << 4,
	 .gain_min = 1 << 4,
	 .gain_max = 2000 << 4,
	 .regs = sensor_1080p30wdr_regs,
	 .regs_size = ARRAY_SIZE(sensor_1080p30wdr_regs),
	 .set_size = NULL,
	 },

	 {
	 .width = 1920,
	 .height = 1080,
	 .hoffset = 0,
	 .voffset = 0,
	 .hts = 2894,
	 .vts = 1104,
	 .pclk = 159770880,
	 .mipi_bps = 176 * 1000 * 1000,
	 .fps_fixed = 25,
	 .bin_factor = 1,
	 .if_mode = MIPI_VC_WDR_MODE,
	 /* .if_mode = MIPI_DOL_WDR_MODE,
	 .wdr_mode = ISP_DOL_WDR_MODE,
	 .intg_min = 1 << 4,
	 .intg_max = (1284 - 10) << 4,
	 .gain_min = 1 << 4,
	 .gain_max = 2000 << 4,
	 .regs = sensor_1080p25wdr_regs,
	 .regs_size = ARRAY_SIZE(sensor_1080p25wdr_regs),
	 .set_size = NULL,
	 },
*/
	};

#define N_WIN_SIZES (ARRAY_SIZE(sensor_win_sizes))

static int sensor_g_mbus_config(struct v4l2_subdev *sd,
				struct v4l2_mbus_config *cfg)
{
	struct sensor_info *info = to_state(sd);

	cfg->type  = V4L2_MBUS_CSI2_DPHY;
	if (info->isp_wdr_mode == ISP_DOL_WDR_MODE)
		cfg->flags = 0 | V4L2_MBUS_CSI2_2_LANE | V4L2_MBUS_CSI2_CHANNEL_0 | V4L2_MBUS_CSI2_CHANNEL_1;
	else
		cfg->flags = 0 | V4L2_MBUS_CSI2_2_LANE | V4L2_MBUS_CSI2_CHANNEL_0;

	return 0;
}

static int sensor_g_ctrl(struct v4l2_ctrl *ctrl)
{
	struct sensor_info *info =
			container_of(ctrl->handler, struct sensor_info, handler);
	struct v4l2_subdev *sd = &info->sd;

	switch (ctrl->id) {
	case V4L2_CID_GAIN:
		return sensor_g_gain(sd, &ctrl->val);
	case V4L2_CID_EXPOSURE:
		return sensor_g_exp(sd, &ctrl->val);
	}
	return -EINVAL;
}

/* static int sensor_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
{
	/* Fill in min, max, step and default value for these controls. */
	/* see include/linux/videodev2.h for details */
	/* switch (qc->id) {
	case V4L2_CID_GAIN:
		return v4l2_ctrl_query_fill(qc, 1 * 16, 128 * 16 - 1, 1, 16);
	case V4L2_CID_EXPOSURE:
		return v4l2_ctrl_query_fill(qc, 1, 65536 * 16, 1, 1);
	}
	return -EINVAL;
} */

static int sensor_s_ctrl(struct v4l2_ctrl *ctrl)
{


	struct sensor_info *info =
			container_of(ctrl->handler, struct sensor_info, handler);
	struct v4l2_subdev *sd = &info->sd;

	switch (ctrl->id) {
	case V4L2_CID_GAIN:
		return sensor_s_gain(sd, ctrl->val);
	case V4L2_CID_EXPOSURE:
		return sensor_s_exp(sd, ctrl->val);

	case V4L2_CID_HFLIP:
		return sensor_s_hflip(sd, ctrl->val);
	case V4L2_CID_VFLIP:
		return sensor_s_vflip(sd, ctrl->val);
	}
	return -EINVAL;
}

static int sensor_reg_init(struct sensor_info *info)
{
	int ret;
	struct v4l2_subdev *sd = &info->sd;
	struct sensor_format_struct *sensor_fmt = info->fmt;
	struct sensor_win_size *wsize = info->current_wins;
	data_type rdval;

	/* sensor_err("ARRAY_SIZE(sensor_default_regs)=%d\n", */
			/* (unsigned int)ARRAY_SIZE(sensor_default_regs)); */

	ret = sensor_write_array(sd, sensor_default_regs,
				 ARRAY_SIZE(sensor_default_regs));
	if (ret < 0) {
		sensor_err("write sensor_default_regs error\n");
		return ret;
	}

	sensor_dbg("sensor_reg_init\n");


	sensor_write_array(sd, sensor_fmt->regs, sensor_fmt->regs_size);

	sensor_flip_status = 0x01;

	if (wsize->regs) {
		/* sensor_err("%s: start sensor_write_array(wsize->regs)\n", __func__); */
		sensor_write_array(sd, wsize->regs, wsize->regs_size);
	}

	if (wsize->set_size)
		wsize->set_size(sd);

	info->width = wsize->width;
	info->height = wsize->height;

	sensor_dbg("s_fmt set width = %d, height = %d\n", wsize->width,
			 wsize->height);
	sensor_read(sd, 0x3109, &rdval);
	sensor_print("0x3109 == 0x%x\n", rdval);

	return 0;
}

static int sensor_s_stream(struct v4l2_subdev *sd, int enable)
{
	struct sensor_info *info = to_state(sd);
	sensor_dbg("%s on = %d, %d*%d fps: %d code: %x\n", __func__, enable,
			 info->current_wins->width, info->current_wins->height,
			 info->current_wins->fps_fixed, info->fmt->mbus_code);
	if (!enable)
		return 0;
	return sensor_reg_init(info);
}

/* ----------------------------------------------------------------------- */
static const struct v4l2_ctrl_ops sensor_ctrl_ops = {
	.g_volatile_ctrl = sensor_g_ctrl,
	.s_ctrl = sensor_s_ctrl,
	/* .queryctrl = sensor_queryctrl, */
};

static const struct v4l2_subdev_core_ops sensor_core_ops = {

	.reset = sensor_reset,
	.init = sensor_init,
	.s_power = sensor_power,
	.ioctl = sensor_ioctl,
#if IS_ENABLED(CONFIG_COMPAT)
	.compat_ioctl32 = sensor_compat_ioctl32,
#endif
};

static const struct v4l2_subdev_video_ops sensor_video_ops = {
	.s_stream = sensor_s_stream,
	.g_mbus_config = sensor_g_mbus_config,
};

static const struct v4l2_subdev_pad_ops sensor_pad_ops = {
	.enum_mbus_code = sensor_enum_mbus_code,
	.enum_frame_size = sensor_enum_frame_size,
	.enum_frame_interval = sensor_enum_frame_interval,
	.get_fmt = sensor_get_fmt,
	.set_fmt = sensor_set_fmt,
};

static const struct v4l2_subdev_ops sensor_ops = {
	.core = &sensor_core_ops,
	.video = &sensor_video_ops,
	.pad = &sensor_pad_ops,
};

/* ----------------------------------------------------------------------- */
static struct cci_driver cci_drv[] = {
	{
		.name = SENSOR_NAME,
		/* .addr_width = CCI_BITS_16, */
		.addr_width = CCI_BITS_16,
		.data_width = CCI_BITS_8,
	}, {
		.name = SENSOR_NAME_2,
		/* .addr_width = CCI_BITS_16, */
		.addr_width = CCI_BITS_16,
		.data_width = CCI_BITS_8,
	}
};

static int sensor_init_controls(struct v4l2_subdev *sd, const struct v4l2_ctrl_ops *ops)
{
	struct sensor_info *info = to_state(sd);
	struct v4l2_ctrl_handler *handler = &info->handler;
	struct v4l2_ctrl *ctrl;
	int ret = 0;

	v4l2_ctrl_handler_init(handler, 2);

	 v4l2_ctrl_new_std(handler, ops, V4L2_CID_GAIN, 1 * 1600,
				  256 * 1600, 1, 1 * 1600);

	ctrl = v4l2_ctrl_new_std(handler, ops, V4L2_CID_EXPOSURE, 1,
				  65536 * 16, 1, 1);

				  v4l2_ctrl_new_std(handler, ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
				  v4l2_ctrl_new_std(handler, ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
	if (ctrl != NULL)
		ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;

	if (handler->error) {
		ret = handler->error;
		v4l2_ctrl_handler_free(handler);
	}

	sd->ctrl_handler = handler;

	return ret;
}
static int sensor_dev_id;

static int sensor_probe(struct i2c_client *client,
			const struct i2c_device_id *id)
{
	struct v4l2_subdev *sd;
	struct sensor_info *info;
	int i;

	sensor_print("%s: start\n", __func__);
	info = kzalloc(sizeof(*info), GFP_KERNEL);
	if (info == NULL)
		return -ENOMEM;
	sd = &info->sd;

	if (client) {
		for (i = 0; i < SENSOR_NUM; i++) {
			if (!strcmp(cci_drv[i].name, client->name))
				break;
		}
		cci_dev_probe_helper(sd, client, &sensor_ops, &cci_drv[i]);
	} else {
		cci_dev_probe_helper(sd, client, &sensor_ops, &cci_drv[sensor_dev_id++]);
	}

	sensor_init_controls(sd, &sensor_ctrl_ops);

	mutex_init(&info->lock);

	info->fmt = &sensor_formats[0];
	info->fmt_pt = &sensor_formats[0];
	info->win_pt = &sensor_win_sizes[0];
	info->fmt_num = N_FMTS;
	info->win_size_num = N_WIN_SIZES;
	info->sensor_field = V4L2_FIELD_NONE;
	/* use CMB_PHYA_OFFSET2  also ok */
	info->combo_mode = CMB_TERMINAL_RES | CMB_PHYA_OFFSET3 | MIPI_NORMAL_MODE;
	/* info->combo_mode = CMB_PHYA_OFFSET2 | MIPI_NORMAL_MODE; */
	info->stream_seq = MIPI_BEFORE_SENSOR;
	info->af_first_flag = 1;
	info->exp = 0;
	info->gain = 0;

	sensor_print("%s: end\n", __func__);

	return 0;
}

static int sensor_remove(struct i2c_client *client)
{
	struct v4l2_subdev *sd;
	int i;

	if (client) {
		for (i = 0; i < SENSOR_NUM; i++) {
			if (!strcmp(cci_drv[i].name, client->name))
				break;
		}
		sd = cci_dev_remove_helper(client, &cci_drv[i]);
	} else {
		sd = cci_dev_remove_helper(client, &cci_drv[sensor_dev_id++]);
	}

	kfree(to_state(sd));
	return 0;
}

static const struct i2c_device_id sensor_id[] = {
	{SENSOR_NAME, 0},
	{}
};

static const struct i2c_device_id sensor_id_2[] = {
	{SENSOR_NAME_2, 0},
	{}
};

MODULE_DEVICE_TABLE(i2c, sensor_id);
MODULE_DEVICE_TABLE(i2c, sensor_id_2);

static struct i2c_driver sensor_driver[] = {
	{
		.driver = {
			   .owner = THIS_MODULE,
			   .name = SENSOR_NAME,
			   },
		.probe = sensor_probe,
		.remove = sensor_remove,
		.id_table = sensor_id,
	}, {
		.driver = {
			   .owner = THIS_MODULE,
			   .name = SENSOR_NAME_2,
			   },
		.probe = sensor_probe,
		.remove = sensor_remove,
		.id_table = sensor_id_2,
	},
};
static __init int init_sensor(void)
{
	int i, ret = 0;

	sensor_dev_id = 0;

	for (i = 0; i < SENSOR_NUM; i++)
		ret = cci_dev_init_helper(&sensor_driver[i]);

	sensor_print("%s: ok\n", __func__);


	return ret;
}

static __exit void exit_sensor(void)
{
	int i;

	sensor_dev_id = 0;

	for (i = 0; i < SENSOR_NUM; i++)
		cci_dev_exit_helper(&sensor_driver[i]);
}

VIN_INIT_DRIVERS(init_sensor);
module_exit(exit_sensor);
